 define(['zepto','layer','mylayer'], function ($,layer,mylayer) {
 	var app={
		//公共事件
		init:function(){
			$.setCookie=function(c_name,value,expiredays){
				var exdate=new Date()
				exdate.setDate(exdate.getDate()+expiredays)
				document.cookie=c_name+ "=" +escape(value)+
				((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
			}
			$.getCookie=function(c_name){
				if (document.cookie.length>0){
				  c_start=document.cookie.indexOf(c_name + "=")
				  if (c_start!=-1){ 
				    c_start=c_start + c_name.length+1 
				    c_end=document.cookie.indexOf(";",c_start)
				    if (c_end==-1) c_end=document.cookie.length
				    return unescape(document.cookie.substring(c_start,c_end))
				    } 
				}
				return ""
			}
		    //ajaxpost请求
		    $.postAjax=function(url,data,success,error){
		    	$.ajax({url:url,data:data,type:"post",dataType:"json",success:function(result){
					if(typeof success =="function"){success(result);}
					},error:function(){
						if(typeof error =="function"){error();}
					}
				});
		    };
			var _this=this;
			//返回
			$("#search_page_back").on("click",function(){
				if($(".sort_info").css("display")=="none"){
					$(".hot_search").hide();
					$(".sort_info").show();
				}
				else{
					$(".search_bank").hide();
					$(".warp").show();
				}
			});
			//清除内容
			$("#search_cancel").on("click",function(){
				$("#searchArea").val("");
			});
			//搜索
			$("#searchForm").on("submit",function(evt){
				var obj=$(this).find("#searchArea");
				var keyword=$.trim(obj.val());
				if(keyword==""){
					if (evt && evt.preventDefault) {
						evt.preventDefault();
					} else if (event) {
						event.returnValue = false;
					}
				}
			});
			$(".search_btn").on("click",function(){
				var keyword=$.trim($("#searchArea").val());
				if(keyword!=""){
					$("#searchForm").submit();
				}
			})
			$("#searchArea").on("focus",function(){
				$(".sort_info").hide();
				$(".hot_search").show();
				var search_info=$(this).parent();
				var value=$.trim($(this).val());
				if($("#search_related_list li").size()>0){
					$("#search_related").show();
				}
			});
			$(".search").on("click",function(){
				var e = window.event ||event;
				if (e.stopPropagation) { 
					e.stopPropagation(); 
				} 
				else if (window.event) { 
					window.event.cancelBubble = true; 
				}
			});
			$("body").on("click",function(e){
				$(".search_result").hide();
			})
			$("#searchArea").on("keyup",function(){
				var value=$.trim($(this).val());
				$(this).attr("actual_value",value);
				var search_info=$(this).parent();
				var that=this;
				if(value==""){
					$("#search_related").hide();
					$("#search_related_list").html("");
				}
				else{
					search_info.find(".search_related_list").html("");
					var keyword=value.substring(0,1);
					if(window.localStorage&&typeof window.localStorage.getItem('searchRelated')){
						var searchRelated=JSON.parse(window.localStorage.getItem('searchRelated'))||{};
						if(searchRelated&&searchRelated[keyword]){
							var c_time=new Date().getTime();
							var date3=c_time-searchRelated[keyword]['time'];
							var days=Math.floor(date3/(24*3600*1000));
							//1天内从缓存读取
							if(days<=1){
								_this.loadRelated(search_info,value,searchRelated[keyword]['list']);
								return false;
							}
						}
					}
					$.postAjax("/ajax/keywords",{'keywords':keyword},function(result){
						if(result&&result.length>0){
							var searchRelated=JSON.parse(window.localStorage.getItem('searchRelated'))||[];
							searchRelated[keyword]={"list":result,"time":new Date().getTime()};
							window.localStorage.setItem('searchRelated',JSON.stringify(searchRelated));
						}
						_this.loadRelated(search_info,value,result);
					});
				}
			});
			$("#search_related").on("mouseout",function(){
				$("#searchArea").val($("#searchArea").attr("actual_value"));
			});
			$("#clearhistory").on("click",function(){
				mylayer.showConfirm("Are you sure to clear the search history?",function(){
					if(window.localStorage&&window.localStorage.getItem('search_history')){
		        		window.localStorage.setItem('search_history','[]');
		    		}
			    	else{
			    		$.setCookie('search_history',"");
			    	}
		    		$("#search_history_list").html("");
				});
			});
			if($(".foot_nav").size()>0){
				$("input[type='text'],input[type='password'],textarea").on("focus",function(){
					$(".foot_nav").addClass('static')
				});
				$("input[type='text'],input[type='password'],textarea").on("blur",function(){
					$(".foot_nav").removeClass('static')
				});
			}
			this.loadhistory();
		},
		loadhistory:function(){
			if(window.localStorage&&window.localStorage.getItem('search_history')){
	        	var search_history=JSON.parse(window.localStorage.getItem('search_history'));
		    }
		    else if($.getCookie('search_history')){
				var search_history=JSON.parse($.getCookie('search_history'))?JSON.parse($.getCookie('search_history')):[];
			}
			else{
				var search_history=[];
			}
		    var length=search_history.length<=10?search_history.length:10;
    		if(length>0){
    			var value="";
        		var html="";
	        	for(var i=0;i<length;i++){
	        		value=$.trim(search_history[i]['keyword']);
	        		if(value){
	        			html+='<li><a keyword="'+value+'" class="c_keyword" href="/search?keyword='+value+'">'+value+'</a></li>';
	        		}
	        	}
	        	$("#search_history_list").html(html).attr("load");
    		}
    		else{
    			$("#search_history_list").html("");
    		}
		},
		loadRelated:function(search_info,keyword,result){
			var _this=this;
			var html="";
			var count=0;
			if(result){
				for(var i=0;i<result.length;i++){
					if(count>10){
						break;
					}
					else if(result[i]&&result[i].indexOf(keyword)==0){
						html+='<li><a keyword="'+result[i]+'" href="/search?keyword='+value+'" class="c_keyword">'+result[i]+'</a></li>';
						count++;
					}
				}
			}
			if(html){
				$("#search_related_list").html(html);
				$("#search_related").show();
				_this.listEvent();
			}
			else{
				$("#search_related").hide();
				$("#search_related_list").html("");
			}
		}
	}
	if(typeof app!=undefined){
		app.init();
	}
	return app;
});